package com.globallogic.training.javase.university.member;

import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;

import com.globallogic.training.javase.university.Speciality;

/**
 * This class represents the list of students
 * that were grouped to study together.
 * @author Ivan Moisiienko
 * @version 0.2 2013/05/13
 */
public class Group implements Comparable<Group> {
	
	private String groupId;
	private Speciality spec;
	private Set<Student> students = new TreeSet<Student>();
	
	/**
	 * @param groupId Group ID cannot be null or blank
	 */
	public Group(String groupId) {		
		if (groupId == null) {
			throw new IllegalArgumentException("Group ID is null");
		}
		
		if (StringUtils.isBlank(groupId)) {
			throw new IllegalArgumentException("Group ID is blank");
		}
		
		this.groupId = groupId;
	}
	
	public void setSpeciality(Speciality spec) {
		this.spec = spec;
	}
	
	public Speciality getSpeciality() {
		return spec;
	};
	
	public String getGroupId() {
		return groupId;
	};
	
	public boolean addStudent(Student st) {
		if (st == null) {
			return false;
		}
		
		return students.add(st);
	};
	
	public boolean removeStudent(Student st) {
		return students.remove(st);
	};
	
	public Student getStudent(String fullName) {
		for (Student stud : students) {
			if (stud != null && stud.getFullName().equals(fullName)) {
				return stud;
			}
		}
		return null;
	}
	
	public Set<Student> getStudents() {
		return students;
	}

	public String toString() {
		return "GroupID: " + groupId;
	};
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + groupId.hashCode();
		return result;
	}
	
	@Override
	public boolean equals(Object otherObject) {
		if (this == otherObject) {
			return true;
		}
		if (otherObject == null) {
			return false;
		}
		if (getClass() != otherObject.getClass()) {
			return false;
		}
		
		Group other = (Group) otherObject;
		if (this.groupId == other.groupId) {
			return true;
		}
		return false;
	}
	
	@Override
	public int compareTo(Group otherGroup) {
		String groupId = this.getGroupId();
		String otherGroupId = otherGroup.getGroupId();
		
		return groupId.compareTo(otherGroupId);
	}
	
}
